<?php

namespace App\Console\Commands;

use App\Admin\Models\SiteCompany;
use App\Store\Models\Member\Member;
use App\Store\Models\Message\Rule;
use App\Store\Models\Order\OrderDeposit;
use App\Store\Models\Order\OrderMaintenance;
use App\Store\Models\Order\OrderPointschangebalance;
use App\Store\Models\Order\OrderRecharge;
use App\Store\Models\Order\OrderRecovery;
use App\Store\Models\Order\OrderSales;
use App\Store\Services\Message\Lianlu;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

/**
 * 回访用户
 * 三天、三周、三月
 */
class GetReturnVisitMember extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:GetReturnVisitMemberList';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取回访用户列表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info('获取回访用户列表开始');
        $this->getrulelist(1);
        $this->getrulelist(2);
        $this->getrulelist(3);
        Log::info('获取回访用户列表结束');
        return 0;
    }

    public function getrulelist($type)
    {
        $list = Rule::where(['type' => $type, 'status' => 2, 'is_del' => 1])->get();
        if ($list) {
            $list = $list->toArray();
            foreach ($list as $rule) {
                $this->getVisitlist($type, $rule);
            }
        } else {
            Log::info('没有'.$type.'回访规则');
        }
    }

    public function getVisitlist($type, $rule)
    {
        if ($type == 1) {
            $day = 3;
        } elseif ($type == 2) {
            $day = 21;
        } else {
            $day = 90;
        }
        //时间戳
        $time = mktime(0, 0, 0, date("m"), date("d") - $day, date("Y"));
        $data = date('Y-m-d', $time);
        $data_start = strtotime($data . ' 00:00:00');
        $data_end = strtotime($data . ' 23:59:59');
        Log::info($data_start.'-'.$data_end);

        //定金
        $depositlist = OrderDeposit::where([
            'audit_status' => 2,
            'is_del' => 1,
        ])->where('create_at', '>=', $data_start)
            ->where('create_at', '<=', $data_end)->get()->toArray();
        $this->sendMessage($depositlist, $rule, '定金');

        //维修
        $maintenancelist = OrderMaintenance::where([
            'audit_status' => 2,
            'is_del' => 1,
        ])->where('create_at', '>=', $data_start)
            ->where('create_at', '<=', $data_end)->get()->toArray();
        $this->sendMessage($maintenancelist, $rule, '维修');

        //积分转预存
        $pointlist = OrderPointschangebalance::where([
            'audit_status' => 2,
            'is_del' => 1,
        ])->where('create_at', '>=', $data_start)
            ->where('create_at', '<=', $data_end)->get()->toArray();
        $this->sendMessage($pointlist, $rule, '积分转预存');

        //充值
        $rechargelist = OrderRecharge::where([
            'audit_status' => 2,
            'is_del' => 1,
        ])->where('create_at', '>=', $data_start)
            ->where('create_at', '<=', $data_end)->get()->toArray();
        $this->sendMessage($rechargelist, $rule, '充值');

        //纯回收
        $recoverylist = OrderRecovery::where([
            'audit_status' => 2,
            'is_del' => 1,
        ])->where('create_at', '>=', $data_start)
            ->where('create_at', '<=', $data_end)->get()->toArray();
        $this->sendMessage($recoverylist, $rule, '纯回收');

        //普通销售
        $saleslist = OrderSales::where([
            'audit_status' => 2,
            'is_del' => 1,
        ])->where('create_at', '>=', $data_start)
            ->where('create_at', '<=', $data_end)->get()->toArray();
        $this->sendMessage($saleslist, $rule, '普通销售');
    }

    public function sendMessage($list, $rule, $type)
    {
        if (!$list) {
            Log::info('获取【'.$type.'】回访用户列表为空');
            return false;
        }

        $phone_arr = [];
        foreach ($list as $item) {
            $one = Member::where(['id' => $item['mem_id'],'is_del'=>1])->first();
            if (!empty($one)) {
                $one = $one->toArray();
                if(!empty($one['mobile'])) {
                    Log::info('mobile:'.$one['mobile']);
                    $phone_arr[] = $one['mobile'];
                }
            }
        }

        if (!empty($phone_arr)) {
            $companyinfo = SiteCompany::where([['id', $rule['cp_id']],['is_open_sms',2]])->first();
            if(!$companyinfo){
                return response()->json(['code' => '001', 'msg' => '短信未开启']);
            }
            Log::info('手机号：'.json_encode($phone_arr));
            Log::info('短信内容：'.$rule['content']);
            Log::info('公司信息：'.json_encode($companyinfo));
            $res = Lianlu::NormalSend($phone_arr, $rule['content'], $companyinfo);
            info('回访短信发送结果' . $res);
            return true;
        } else {
            Log::info('公司ID：'.$rule['cp_id'].'，门店ID：'.$rule['store_id'].'，用户手机号为空');
            return 0;
        }
    }

}
